<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" @click="addFenlei">添加分类</el-button>
      <tree-table
        :data="dataList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :showIndex="true"
        :border="true"
        index-text="#"
      >
        <!-- 是否有效 -->
        <template slot="empower" slot-scope="scoped">
          <i
            class="el-icon-success"
            v-if="scoped.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <!-- 排序 -->
        <template slot="paixu" slot-scope="scoped">
          <el-tag v-if="scoped.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-if="scoped.row.cat_level === 1">二级</el-tag>
          <el-tag type="info" v-if="scoped.row.cat_level === 2">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="cz" slot-scope="scoped">
          <el-button type="success" size="mini" @click="edit(scoped.row)"
            >编辑</el-button
          >
          <el-button type="danger" size="mini" @click="deletes(scoped.row)"
            >删除</el-button
          >
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <EditLog
      :Editshows="Editshow"
      :EditData="EditDatas"
      @closeEdit="closeEdits"
      @updataEdit="getData"
    ></EditLog>
    <DiaLog
      :Diashows="Diashow"
      @closeDiag="closeDiags"
      :data="dataLists"
      @updata="getData"
    ></DiaLog>
  </div>
</template>

<script>
import Dialog from "./Dialog";
import Editlog from "./EditDialog";
export default {
  data() {
    return {
      dataList: [],
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      dataLists: [],
      total: 0,
      EditDatas: {},
      Editshow: false,
      Diashow: false,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          type: "template",
          template: "empower",
        },
        {
          label: "排序",
          type: "template",
          template: "paixu",
        },
        {
          label: "操作",
          type: "template",
          template: "cz",
        },
      ],
    };
  },
  methods: {
    getData() {
      this.$axios.get("categories", { params: this.querInfo }).then((res) => {
        console.log(res);
        if (res.data.meta.status == 200) {
          this.dataList = res.data.data.result;
          this.total = res.data.data.total;
        } else {
          this.$message({
            message: res.data.meta.msg,
            type: "error",
          });
        }
      });
    },
    //删除
    deletes(val) {
      this.$messageBox("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios.delete(`categories/${val.cat_id}`).then((res) => {
            if (res.data.meta.status == 200) {
              this.$message({
                type: "success",
                message: res.data.meta.msg,
              });
              this.getData();
            } else {
              this.$message({
                type: "error",
                message: res.data.meta.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //编辑
    edit(val) {
      this.EditDatas = val;
      this.Editshow = true;
      console.log(val);
    },
    closeEdits() {
      this.Editshow = false;
    },
    handleSizeChange(newsize) {
      this.querInfo.pagesize = newsize;
      this.getData();
    },
    handleCurrentChange(newpage) {
      this.querInfo.pagenum = newpage;
      this.getData();
    },
    //添加分类
    addFenlei() {
      this.$axios.get("categories", { params: { type: 2 } }).then((res) => {
        this.dataLists = res.data.data;
        console.log(res);
      });
      this.Diashow = true;
    },
    //关闭弹窗
    closeDiags() {
      this.Diashow = false;
    },
  },
  created() {
    this.getData();
  },
  components: {
    DiaLog: Dialog,
    EditLog: Editlog,
  },
};
</script>
<style lang='scss' scoped>
</style>